package com.zhuangxiaoyan.designpattern.singletonmodel;

/**
 * 使用的懒汉模式 会导致的出现线程不安全问题（多线程时候导致访问的不是一个对象。从而导致的安全问题）
 */
public class SingletonDemo {

    private SingletonDemo instance;

    private SingletonDemo() {
    }

    public SingletonDemo getInstance() {
        if (instance == null) {
            instance = new SingletonDemo();
        }
        return instance;
    }


    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                    SingletonDemo Singletondemo = new SingletonDemo().getInstance();
                    System.out.println(Singletondemo);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(100);
                    SingletonDemo Singletondemo = new SingletonDemo().getInstance();
                    System.out.println(Singletondemo);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
